package com.example.demo_videos.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.example.demo_videos.mapper.VideoInfoMapper;
import com.example.demo_videos.pojo.VideoInfo;
import com.example.demo_videos.service.VideoInfoService;
import com.example.demo_videos.utils.ConstantVod;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static com.example.demo_videos.utils.InitObject.initVodClient;


@Service
public class VideoInfoServiceImpl implements VideoInfoService {

    @Resource
    private VideoInfoMapper videoInfoMapper;

    /**
     * 上传视频到aliyun
     *
     * @param file
     * @return
     */
    @Override
    public Integer uploadVideoAliyun(MultipartFile file) {
        Integer result = 0;
        try {
            String originalName = file.getOriginalFilename();
            //上传之后文件的标题
            String title = originalName.substring(0, originalName.lastIndexOf("."));
            //文件名称
            String fileName = originalName;
            //上传文件流
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(ConstantVod.ACCESS_KEY_ID, ConstantVod.ACCESS_KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
            if (response.isSuccess()) {
                VideoInfo videoInfo = new VideoInfo();
                videoInfo.setName(fileName);
                videoInfo.setVideoId(response.getVideoId());
                result = videoInfoMapper.insert(videoInfo);
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 视频播放
     * @param vid
     * @return
     */
    @Override
    public String getPlayAuth(String vid){
        String playAuth = null;
        try {
            //播放凭证
            DefaultAcsClient client = initVodClient(ConstantVod.ACCESS_KEY_ID, ConstantVod.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            request.setVideoId(vid);
            response = client.getAcsResponse(request);
            playAuth = response.getPlayAuth();
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        return playAuth;
   }

    @Override
    public List getVideoList() {
        return videoInfoMapper.selectList(null);
    }
}



